package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysInformation;
import com.ruoyi.system.domain.SysProjectAlarm;
import com.ruoyi.system.mapper.SysCheckMapper;
import com.ruoyi.system.mapper.SysInformationMapper;
import com.ruoyi.system.mapper.SysProjectAlarmMapper;
import com.ruoyi.system.service.ISysInformationService;
import com.ruoyi.system.service.SysAlarmService;
import org.apache.ibatis.annotations.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
*
* @Author zhaoguanyu
* @Date 16:27 2021/11/29
*/
@Service
public class SysInformationServiceImpl implements ISysInformationService {

    @Autowired
    private SysInformationMapper sysInformationMapper;
    @Autowired
    private SysAlarmService sysAlarmService;

    /**
     * 校验项目名称是否唯一
     *
     * @param sysInformation 项目信息
     * @return 结果
     */
    @Override
    public String checkItemNameUnique(SysInformation sysInformation) {
        //查询库里是否有该名称的数据
        SysInformation info = sysInformationMapper.checkItemNoUnique(sysInformation.getItemName());
        if (!StringUtils.isNull(info) && info.getItemName().equals(sysInformation.getItemName())){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }



    /**
     * 校验项目编号是否唯一
     *
     * @param sysInformation 项目信息
     * @return 结果
     */
    @Override
    public String checkItemNoUnique(SysInformation sysInformation) {
        //查询库里是否有该编码的数据
        SysInformation info = sysInformationMapper.checkItemNameUnique(sysInformation.getItemNo());
        //新增的编号与库里重复
        if (!StringUtils.isNull(info) && info.getItemNo().equals(sysInformation.getItemNo())){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增保存项目信息
     *
     * @param sysInformation 岗位信息
     * @return 结果
     */
    @Override
    public int insertInformation(SysInformation sysInformation) {
        return sysInformationMapper.insertInformation(sysInformation);
    }


    /**
     * 修改保存项目信息
     *
     * @param sysInformation 项目信息
     * @return 结果
     */
    @Override
    public int updateInformation(SysInformation sysInformation) {
        Date readTime = sysInformation.getReadTime();
        Date importTime = sysInformation.getImportTime();
        if (readTime != null && importTime != null){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(readTime);
            calendar.setTime(importTime);
            calendar.add(Calendar.DATE,1);
            sysInformation.setReadTime(calendar.getTime());
            sysInformation.setImportTime(calendar.getTime());
        }
        return sysInformationMapper.upDateSysInformation(sysInformation);
    }


    /**
     * 删除项目
     *
     * @param id 项目编码
     * @return 结果
     */
    @Override
    public int deleteSys(Long[] id) {
        return sysInformationMapper.deleteSys(id);
    }

    /**
     * 查询项目信息集合
     *
     * @param sysInformation 项目信息
     * @return 项目信息集合
     */
    @Override
    public List<SysInformation> selectPostList(SysInformation sysInformation) {
        List<SysInformation> sysList = sysInformationMapper.selectSysList(sysInformation);
        Iterator<SysInformation> it = sysList.iterator();
        List list = new ArrayList<>();
        while (it.hasNext()) {
            SysInformation next = it.next();
            Date endTime = next.getEndTime();
            Date date = new Date();
            if(null!=endTime) {
                int days = (int) ((date.getTime() - endTime.getTime()) / (1000 * 3600 * 24));
                if (days > 0){
                    next.setColor("#F56C6C");
                    sysInformationMapper.upDateSysInformation(next);
                }
            }
            if (StringUtils.isNotEmpty(next.getProjecters())){
                String sss = next.getProjecters();
                List<Long> collect = Arrays.stream(sss.split(",")).map(Long::valueOf).collect(Collectors.toList());
                next.setSsss(collect);
            }
            list.add(next);

        }
        return list;

    }

    @Override
    public List<SysInformation> selectProjectorList(Long ProjectorId) {
        List<SysInformation> sysInformations = sysInformationMapper.selectProjectorList(ProjectorId);
        return sysInformations;
    }


}
